<?php

namespace App\Exports;

use App\Commission;
use App\GroupBuyItem;
use App\Merchant;
use App\Order;
use App\OrderDetail;
use App\Sku;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Cell\StringValueBinder;

class CommissionExport extends StringValueBinder implements FromCollection, WithHeadings,WithCustomValueBinder
{
    public $start_time;
    public $end_time;

    public function __construct($start_time, $end_time)
    {
        $this->start_time = $start_time;
        $this->end_time = $end_time;
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        $end_time=$this->end_time;
        $start_time=$this->start_time;
        $arr = collect([]);
        $result = Commission::with(['user', 'promotion'])->when($this->start_time,function ($query)use ($start_time){
            $query->where('created_at','>=',$start_time);
        })->when($end_time,function ($query)use ($end_time){
            $query->where('created_at','<=',$this->end_time);
        })->latest()->where('type', 0)->get();
        foreach ($result as $key => $value) {
            $detail = OrderDetail::where('order_id', $value['order_number'])->get();
            $merchant = Merchant::where('uid',$value['uid'])->first();
            foreach ($detail as $k => $v) {
                $money = 0;
                if ($v["sku_id"]) {
                    $money = Sku::find($v["sku_id"])["price"];
                } else {
                    $money = $v["origin_price"];
                }
                $sku=$v['sku'];
                if (strpos($v["sku"],"#")!==false){
                    $sku=substr($v["sku"],7);
                }
                $data = [
                    "promotion_name" => isset($value->promotion)?$value->promotion->nickname:"用户已删除",
                    "uid" => $value["uid"],
                    "fans_name" => isset($value->user)?$value->user->nickname:"",
                    'order_uid' => $value->order_uid,
                    "order_number" => $v['order_id'],
                    "goods_name" => $v["goods_name"],
                    "sku" => $sku,
                    "number" => $v["number"],
                    "origin_price" => $money,
                    "pay_detail_price" => $v['price'] * $v['number'],
                    "pay_time" => $value['created_at'],
                    "pay_status" => 1,
                    "commission" => ceil($merchant->commission) > 0 ? $v['number'] * $merchant->commission : $v['price'] * $v['number'] * $merchant->rate / 100


                ];
                $arr->push($data);
            }



        }
        return $arr;
    }

    public function headings(): array
    {
        return [
            "推广大使微信昵称",
            "推广大使ID",
            "粉丝微信昵称",
            "粉丝ID",
            "订单号",
            "商品名称",
            "规格",
            "数量",
            "商品原价",
            "支付金额",
            "支付时间",
            "支付状态",
            "佣金金额",
        ];
    }
}
